
import java.io.FileOutputStream;
import java.util.Date;

import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class FirstPdf {
    private static int z = 0;
    private static String FILE = "./FirstPdf.pdf";

    private static Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
					     Font.BOLD);
    private static Font summaryFont = new Font(Font.FontFamily.TIMES_ROMAN,
					       18, Font.NORMAL);
    private static Font normalbold = new Font(Font.FontFamily.TIMES_ROMAN, 12, 
					      Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
					   Font.NORMAL, BaseColor.RED);
    private static Font blueFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
					    Font.NORMAL, BaseColor.BLUE);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
					   Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
					     Font.BOLD);

    public static void go(Building activeBuilding,String in){
	try {
	    Document document = new Document(PageSize.LETTER);
	    PdfWriter.getInstance(document, new FileOutputStream(in));

	    document.open();

	    /*
	     * add title, add subject, add keyword, author, creator
	     */
	    addMetaData(document);
	    addJobInfo(document, activeBuilding);
	    addZones(document, activeBuilding);
			
	    document.close();

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
	
    private static void addMetaData(Document document) {
	document.addTitle("Heat Gain/Loss Information");
	document.addSubject("Calculate Heat Loss and Gain");
	document.addKeywords("Java, PDF, Heat Loss, Heat Gain, Calculator");
    }
    private static void addJobInfo(Document document, Building activeBuilding) throws DocumentException {
	Paragraph parOne = new Paragraph();
	Paragraph parTwo = new Paragraph();
	Paragraph parThree = new Paragraph(); 
	
		
	parOne.setAlignment(Element.ALIGN_CENTER);
	parOne.add(new Paragraph("Maxwell's Calculator Report", titleFont));
	// Will create: Report generated by: _name, _date
	parOne.add(new Paragraph("Report generated by: "
				 + System.getProperty("user.name") + ", " + 
				 new Date(), smallBold));
		
	addEmptyLine(parOne, 1);
		
	parTwo.setAlignment(Element.ALIGN_LEFT);
	parTwo.add(new Paragraph("Job Name:    " +activeBuilding.getJobName()));
	parTwo.add(new Paragraph("Job Number:    " 
				 + activeBuilding.getJobNumber()));
	parTwo.add(new Paragraph("Job Date:    " +activeBuilding.getJobDate()));
	addEmptyLine(parTwo, 1);
	parTwo.add(new Paragraph("Customer:    "+activeBuilding.getCustomer()));
	parTwo.add(new Paragraph("Address:\n" + activeBuilding.getAddress()));
	addEmptyLine(parTwo, 1);
	parTwo.add(new Paragraph("Region:    " 
				   + activeBuilding.getRegionName() 
				   + "  ("  + activeBuilding.getRegionNumber()
				   + ")"));
	addEmptyLine(parTwo, 3);
		
	parThree.setAlignment(Element.ALIGN_CENTER);
	parThree.add(new Paragraph("Total Gain:  " + 
				   (int)activeBuilding.getTotalGain() + " BTU"
				   ,summaryFont ));
	addEmptyLine(parThree, 1);
	parThree.add(new Paragraph("Total Loss:  "  +  
				   (int)activeBuilding.getTotalLoss() + " BTU"
				   , summaryFont));
	addEmptyLine(parThree, 3);
	parThree.add(new Paragraph("Notes:"));
	addEmptyLine(parThree, 1);
	parThree.add(new Paragraph(activeBuilding.getNotes()));
	
	document.add(parOne);	
	document.add(parTwo);
	document.add(parThree);
	// Start a new page
	document.newPage();
    }

    private static void addZones(Document doc, Building active)
	throws DocumentException {
	int zone=0;
    /** FIX FOR INDEX OUT OF BOUNDS EXCEPTION EXPERIENCED WHEN TRYING TO PRINT
        MORE THAN ONCE IN ONE PROGRAM EXECUTION **/
    z = 0;
		
	Anchor anchor = new Anchor("Zone" + zone, titleFont);
	anchor.setName("Heat Loss and Gain Calculator");
	Chapter catPart = new Chapter(new Paragraph(anchor), 1);
		
		
	for (zone = 0; zone < active.numZones(); zone++) {
			
	    // Second parameter is the number of the chapter
			
	    anchor = new Anchor(active.getZoneTitle(zone), titleFont);
	    anchor.setName("Heat Loss and Gain Calculator");
	    catPart = new Chapter(new Paragraph(anchor), zone+1);
			
			 
	    Paragraph subPara = new Paragraph("Heat Gain", subFont);
	    //Section subCatPart = catPart.addSection(subPara);
	    addEmptyLineChap(catPart, 2);
	    addGain(doc, active, catPart);
	    //addEmptyLine(subPara, 4);

	    //subPara = new Paragraph("Heat Loss", subFont);
	    //subCatPart = catPart.addSection(subPara);
	    //addLoss(doc, active, subCatPart);
	    //addEmptyLine(subPara, 4);	
	    z++;

			
	    doc.add(catPart);
	}

		 
    }
    private static void addGain(Document doc, Building active, Chapter catPart){
	PdfPTable table = new PdfPTable(5);

	PdfPCell c1 = new PdfPCell(new Phrase(" "));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);

	c1 = new PdfPCell(new Phrase("Area"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);

	c1 = new PdfPCell(new Phrase("Height"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);

	c1 = new PdfPCell(new Phrase("Gain"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
     
	c1 = new PdfPCell(new Phrase("Loss"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
		
		
		
		
		
	table.setHeaderRows(1);

	table.addCell("Gross Wall");
	table.addCell(active.getZoneData(z, 0, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
		
	table.addCell("North Window");
	table.addCell(active.getZoneData(z, 1, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 1, 3) + " BTU");
	table.addCell("+");
		
	table.addCell("South Window");
	table.addCell(active.getZoneData(z, 2, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 2, 3) + " BTU");
	table.addCell("+");
		
	table.addCell("East/West Window");
	table.addCell(active.getZoneData(z, 3, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 3, 3) + " BTU");
	table.addCell("+");

		
	table.addCell("SkyLight");
	table.addCell(active.getZoneData(z, 4, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 4, 3) + " BTU");
	table.addCell(active.getZoneData(z,20,3) + " BTU");
		
	table.addCell("Doors");
	table.addCell(active.getZoneData(z, 5, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 5, 3) + " BTU");
	table.addCell(active.getZoneData(z,21,3) + " BTU");
		
	table.addCell("Net Wall");
	table.addCell(active.getZoneData(z, 6, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 6, 3) + " BTU");
	table.addCell(active.getZoneData(z,22,3) + "BTU");
		
	table.addCell("Ceiling");
	table.addCell(active.getZoneData(z, 7, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 7, 3) + " BTU");
	table.addCell(active.getZoneData(z,23,3) + " BTU");
		
	table.addCell("Floor");
	table.addCell(active.getZoneData(z, 8, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 8, 3) + " BTU");
	table.addCell(active.getZoneData(z,24,3) + " BTU");
		
	table.addCell("Infiltration");
	table.addCell(active.getZoneData(z, 9, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z, 9, 1) + " ft.");
	table.addCell(active.getZoneData(z, 9, 3) + " BTU");
	table.addCell(active.getZoneData(z,25,3) + " BTU");
		
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
	table.addCell("Number of People");
	table.addCell(""+active.getZoneData(z, 10, 0));
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 10, 3) + " BTU");
	table.addCell("  ");
		
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
		
	table.addCell("Appliance");
	table.addCell(active.getZoneData(z, 11, 0) + " BTU");
	table.addCell("  ");
	table.addCell(active.getZoneData(z, 11, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		

	Paragraph subtotals = new Paragraph("SUBTOTALS", normalbold);
	Paragraph subgain = new Paragraph(active.getZoneData(z,12,3) + " BTU");
	Paragraph subloss = new Paragraph(active.getZoneData(z,26,3) + " BTU");
	table.addCell(subtotals);
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(subgain);
	table.addCell(subloss);
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
	table.addCell("Duct Loss");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,13,3) + " BTU");
	table.addCell("  ");
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
	table.addCell("Total Sensible Gain");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,14,3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Latent Infiltration");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,15,3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Latent People Gain");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,16,3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Total Latent Gain");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,17,3) + " BTU");
	table.addCell("  ");
		
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
	table.addCell("  ");
		
	Paragraph totals = new Paragraph("TOTALS", normalbold);
	Paragraph gain = new Paragraph(active.getZoneData(z,12,3) + " BTU");
	Paragraph loss = new Paragraph(active.getZoneData(z,26,3) + " BTU");
	table.addCell(totals);
	table.addCell("  ");
	table.addCell("  ");
	table.addCell(gain);
	table.addCell(loss);

	catPart.add(table);
    }
	
    private static void addLoss(Document doc, Building active, 
				Section subCatPart){

	PdfPTable table = new PdfPTable(4);
	
	PdfPCell c1 = new PdfPCell(new Phrase(" "));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
	
	c1 = new PdfPCell(new Phrase("Area"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
	
	c1 = new PdfPCell(new Phrase("Loss"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
	
	c1 = new PdfPCell(new Phrase("Height"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
	
	
	table.addCell("Gross Wall");
	table.addCell(active.getZoneData(z,19, 0) + " sq.ft.");
	table.addCell("  ");
	table.addCell("  ");
	
	table.addCell("All Windows");
	table.addCell(active.getZoneData(z,20, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z,20, 3) + " BTU");
	table.addCell("  ");

	table.addCell("Doors");
	table.addCell(active.getZoneData(z,21, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z,21, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Net Wall");
	table.addCell(active.getZoneData(z,22, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z,22, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Ceiling");
	table.addCell(active.getZoneData(z,23, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z,23, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Floor");
	table.addCell(active.getZoneData(z,24, 0) + " sq.ft.");
	table.addCell(active.getZoneData(z,24, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Infiltration");
	table.addCell(""+active.getZoneData(z,25, 0) + " sq.ft");
	table.addCell(""+active.getZoneData(z,25, 3) + " BTU");
	table.addCell(""+active.getZoneData(z,25, 1) + " ft.");
		
	table.addCell("Heat Loss Subtotal");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,26, 3) + " BTU");
	table.addCell("  ");
		
	table.addCell("Total Loss");
	table.addCell("  ");
	table.addCell(active.getZoneData(z,27, 3) + " BTU");
	table.addCell("  ");

	subCatPart.add(table);
    }
	
    private static void addEmptyLine(Paragraph paragraph, int number) {
	for (int i = 0; i < number; i++) {
	    paragraph.add(new Paragraph(" "));
	}
    }

    private static void addEmptyLineChap(Chapter chapter, int number) {
	for (int i = 0; i < number; i++) {
	    chapter.add(new Paragraph(" "));
	}
    }
}
